package servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import service.UserService;
import util.RegexUtil;

public class User extends HttpServlet {
	UserService userService=new UserService();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String action=request.getParameter("who");
		if(action!=null){
			if(action.equals("me"))	{
				HttpSession session = request.getSession();
				if(session!=null){					
					if(session.getAttribute("user")!=null){
						domain.User user=(domain.User)session.getAttribute("user");
						request.setAttribute("user", user);
					}
				}
			}
			else{
				if(RegexUtil.isParsableToInt(action)){
					long uid=Long.parseLong(action);
					domain.User user=userService.getUserByUid(uid);
					request.setAttribute("user", user);
				}
				else{
					response.sendRedirect("home");
				}
			}
		}
		else{
			response.sendRedirect("home");
		}
		
		if(request.getAttribute("user")!=null)		{
			RequestDispatcher dispatcher = request.getRequestDispatcher("jsp/user/user-show.jsp");
			if (dispatcher != null) dispatcher.forward(request, response);
		}
		else{
			response.sendRedirect("home");
		}			
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
